﻿using BootstrapBlazor.OnlyServerZhongShun.Data.dbmodels;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;

namespace BootstrapBlazor.OnlyServerZhongShun.Components.Pages.中顺
{
    public class AccountController : Controller
    {

        public SqlSugarScope _SqlSugarScope { get; set; }
        public AccountController(SqlSugarScope sqlSugarScope)
        {
            _SqlSugarScope = sqlSugarScope;
        }

        [HttpGet]
        public IResult Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username) == true || string.IsNullOrEmpty(password) == true)
            {
				return Results.Redirect("/login");
			}
            Sys_User User = _SqlSugarScope.Queryable<Sys_User>()
                                            .Where(it => it.UserName == username && it.UserPwd == EncryptDES(password, "C5ABA9E202D94C43A3CA66002BF77FAF"))
            .First();

            if (User != null)
            {
                //查角色
                Sys_Role sys_Role = _SqlSugarScope.Queryable<Sys_Role>().Where(it => it.Role_Id == User.Role_Id).First();
                var identity = new ClaimsIdentity(new[]
               {
                    new Claim(ClaimTypes.Name, username),
                    new Claim(ClaimTypes.Role, sys_Role.RoleName)
                }, CookieAuthenticationDefaults.AuthenticationScheme);

                ClaimsPrincipal _currentUser = new ClaimsPrincipal(identity);

                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, _currentUser);
                return Results.Redirect("/index");
            }
            else
            {
                return Results.Redirect("/login");
            }

        }

        public string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 16));
                byte[] rgbIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

                using (var DCSP = Aes.Create())
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(inputByteArray, 0, inputByteArray.Length);
                            cStream.FlushFinalBlock();
                            return Convert.ToBase64String(mStream.ToArray()).Replace('+', '_').Replace('/', '~');
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("密码加密异常" + ex.Message);
            }

        }

        [HttpGet]
        public IResult Logout(string username)
        {
          //退出登录
          HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return Results.Redirect("/login");
        }
    }
}
